var express = require('express'),
    bodyParser = require("body-parser"),
    mysql = require('mysql'),
    cors = require('cors'),
    app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
var connection = mysql.createConnection({
    host: '43.248.133.133',
    user: 'mysql82830687',
    password: 'na-;E)=KS=zc47F',
    port: '3306',
    database: 'mysql82830687_db'
});
connection.connect();
// 增
app.post('/add', (req, res) => {
    req.body=Object.assign(req.body,{date:new Date()});
    console.log(req.body)    
    connection.query('insert into location set ?', req.body, function (err, result) {
        if (err) throw err;
        console.log(result);
        console.log('\n');
        res.send(result)
    });

})
// 删
app.get('/delete', (req, res) => {
    connection.query('delete from  users where name="wangwu"', {}, function (err, result) {
        if (err) throw err;
        console.log('deleted zhangsan');
        console.log(result);
        console.log('\n');
        res.send(result)
    });
})
// 改
app.get('/updata', (req, res) => {
    connection.query('update users set password="aaa" where name="zhangsan"', { password: 'ppp' }, function (err, result) {
        if (err) throw err;
        console.log('updated zhangsan\'s password to ddd');
        console.log(result);
        console.log('\n');
        res.send(result)
    });

})
// 查
app.get('/select', (req, res) => {
    connection.query('select * from users', { name: 'zhangsan' }, function (err, rows, fields) {
        if (err) throw err;

        console.log('selected after deleted');
        for (var i = 0, usr; usr = rows[i++];) {
            console.log('user nae=' + usr.name + ', password=' + usr.password);
        }
        res.send(rows);
    });

})
app.listen(1800, () => {
    console.log('1800')
})